﻿using System;
using NineSeas.Models;
using Orchard.ContentManagement.Drivers;
using Orchard.FileSystems.Media;
using Orchard.Services;
namespace NineSeas.Drivers
{
    public class BookPartDriver : ContentPartDriver<BookPart>
    {
        private readonly IClock _clock;
        private readonly IStorageProvider _storageProvider;
        public BookPartDriver(IClock clock,
                IStorageProvider storageProvider)
        {
            this._clock = clock;
            this._storageProvider = storageProvider;
        }

        protected override string Prefix
        {
            get
            {
                return "Book";
            }
        }
        protected override DriverResult Display(BookPart part, string displayType, dynamic shapeHelper)
        {
            return Combined(
                ContentShape("Parts_Book", () => shapeHelper.Parts_Book(
                        Book: part
                    )),
                ContentShape("Parts_Book_Summary", () => shapeHelper.Parts_Book_Summary(
                        Book: part
                    )));
        }

        protected override DriverResult Editor(BookPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_Book_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Book",
                    Model: part,
                    Prefix: Prefix));
        }

        protected override DriverResult Editor(BookPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            part.LastUpdatedDate = _clock.UtcNow;
            if (!String.IsNullOrEmpty(part.ImageStoragePath))
            {
                part.ImageUrl = _storageProvider.GetPublicUrl(part.ImageStoragePath);
            }
            return Editor(part, shapeHelper);
        }
    }
}